package android.slc.user.ui.dept.fragment

import android.os.Bundle
import android.slc.appbase.ui.fragment.base.AppMvvmBaseFragment
import android.slc.extras.component.tree.entity.SlcTreeNav
import android.slc.extras.component.tree.ui.TreeNavVm
import android.slc.user.R
import android.slc.user.databinding.UserFragmentDeptListBinding
import android.slc.user.ui.dept.delegate.DeptListMultipleDelegate
import android.slc.user.ui.dept.vm.DeptListMultipleVm
import android.slc.user.ui.dept.vm.DeptMultipleShelfVm
import android.slc.user.ui.dept.vmbox.DeptListMultipleVmBox
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ToastUtils

/**
 *
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/06 下午 3:24
 *
 */
class DeptListMultipleFragment :
    AppMvvmBaseFragment<UserFragmentDeptListBinding, DeptListMultipleVm>() {
    private lateinit var treeNavVm: TreeNavVm
    private lateinit var deptUserMultipleShelfVm: DeptMultipleShelfVm

    private val onPopPropertyChangedCallback: Observable.OnPropertyChangedCallback =
        object : Observable.OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable, propertyId: Int) {
                val slcTreeNav: SlcTreeNav? = (sender as ObservableField<SlcTreeNav?>).get()
                slcTreeNav?.let {
                    if (slcTreeNav == viewModel.slcTreeNavOf.get()) {
                        /*if (slcTreeNav.isPopNoAnimator) {
                            //此处设置alpha为0是为了动画好看
                            contentView.alpha = 0F
                            supportDelegate.popQuiet()
                        } else {
                            pop()
                        }*/
                        ToastUtils.showShort("改变了架构，这里请完善")
                    }
                }
            }
        }

    override fun setContentView(): Any {
        return R.layout.user_fragment_dept_list
    }

    override fun onInitViewModel() {
        super.onInitViewModel()
        this.treeNavVm = getVmByActivityProvider(TreeNavVm::class.java)
        this.deptUserMultipleShelfVm = getVmByActivityProvider(DeptMultipleShelfVm::class.java)
    }

    override fun onBindView(savedInstanceState: Bundle?) {
        super.onBindView(savedInstanceState)
        DeptListMultipleDelegate().apply {
            init(
                this@DeptListMultipleFragment, dataBinding.root,
                viewModel.dataListVmBox as DeptListMultipleVmBox
            )
        }
    }

    override fun onRegisterLiveEvent() {
        super.onRegisterLiveEvent()
        viewModel.dataListVmBox.showTreeClickUiOf.observe(this, object : Observer<Bundle> {
            override fun onChanged(value: Bundle) {
                /*start(DeptListMultipleFragment().apply {
                    arguments = value
                })*/
                ToastUtils.showShort("改变了架构，这里请完善")
            }
        })
        viewModel.slcTreeNavOf.addOnPropertyChangedCallback(object :
            Observable.OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                viewModel.slcTreeNavOf.get()?.let {
                    treeNavVm.addTreeNav(it)
                }
            }
        })
        treeNavVm.getPopOf()
            .addOnPropertyChangedCallback(onPopPropertyChangedCallback)
    }

    override fun onBindViewLater() {
        super.onBindViewLater()
        arguments?.let {
            viewModel.initVm(it, deptUserMultipleShelfVm.treeMultipleShelfVmBox)
        } ?: let {
            ToastUtils.showShort(R.string.user_label_dept_not_found)
            requireActivity().finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        treeNavVm.removeLast()
        treeNavVm.getPopOf().removeOnPropertyChangedCallback(onPopPropertyChangedCallback)
    }
}